import { Component, instantiate, Node, Vec2, Vec3, _decorator } from 'cc';
import { HeatMapPoint3D } from './HeatMapPoint3D';

const { ccclass, property } = _decorator;
export interface IHeatmapPoint2D {
    x: number;
    y: number;
    intensity: number;
}

export interface IHeatmapPoint3D {
    x: number;
    z: number;
    intensity: number;
}

@ccclass('Render3DHeatMapPoint')
export class Render3DHeatMapPoint extends Component {
    @property(Node)
    public heatmapPoint: Node = null;
    @property(Node)
    public heatMap: Node = null;
    @property
    public mapScaleX = 200;

    @property
    public mapScaleZ = 200;

    public start() {
        this.setPlaneMesh();
        let res = this.getHeatPoint();
        console.log('res', res);
        this.createHeatPoint(res);
    }

    public setPlaneMesh() {
        this.heatMap.setScale(this.mapScaleX, 1, this.mapScaleZ);
    }

    public getHeatPoint() {
        const heatMapData = [
            [1, 2, 3, 4, 5, 16, 7, 8, 9, 20],
            [2, 0, 3, 5, 16, 1, 40, 9, 10, 8],
            [0, 0, 3, 4, 6, 7, 30, 9, 10, 8],
            [4, 2, 3, 5, 5, 1, 30, 4, 0, 18],
            [0, 2, 3, 5, 16, 7, 30, 1, 0, 8],
            [6, 2, 3, 5, 6, 17, 1, 39, 10, 0],
        ];

        const gridSizeX = (this.mapScaleX / heatMapData[0].length) * 10;
        const gridSizeZ = (this.mapScaleZ / heatMapData.length) * 10;

        const centerPos = new Vec2(this.mapScaleX / 2, this.mapScaleZ / 2).clone().multiplyScalar(10);
        const startPos = new Vec2(-centerPos.x + gridSizeX / 2, -centerPos.y + gridSizeZ / 2);

        let res: IHeatmapPoint2D[] = [];

        for (let i = 0; i < heatMapData.length; i++) {
            for (let j = 0; j < heatMapData[i].length; j++) {
                let x = gridSizeX * j + startPos.x;
                let y = gridSizeZ * i + startPos.y;
                res.push({ x: x, y: y, intensity: heatMapData[i][j] });
            }
        }
        return res;
    }

    public createHeatPoint(arr: IHeatmapPoint2D[]) {
        arr.forEach((i) => {
            let node = instantiate(this.heatmapPoint);
            this.heatmapPoint.parent.addChild(node);
            node.setPosition(new Vec3(i.x, 1, i.y));
            node.getComponent(HeatMapPoint3D).setIntensity(i.intensity / 40);
            node.active = true;
        });
    }

    public update(deltaTime: number) {}
}
